package com.liu.common.exception.handler;

import cn.hutool.core.util.StrUtil;
import com.liu.common.exception.WarframeBotException;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.util.Objects;

@ControllerAdvice
public class ControllerExceptionHandler {

    @ExceptionHandler(WarframeBotException.class)
    public ResponseEntity<String> exResult(WarframeBotException ex) {
        return ResponseEntity.status(ex.getCode()).body(ex.getMessage());
    }

    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public ResponseEntity<String> exResult(MethodArgumentTypeMismatchException ex, HttpServletRequest request) {
        String groupId = Objects.equals(request.getParameter("groupId"), "null") ? null:request.getParameter("groupId");
        String userId = Objects.equals(request.getParameter("userId"), "null") ? null:request.getParameter("userId");
        if (StrUtil.isEmpty(groupId) || StrUtil.isEmpty(userId)) {
            return ResponseEntity.status(999).body("请先登录");
        }
        return ResponseEntity.status(500).body(ex.getMessage());
    }

}
